package com.normalsys.live.base.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.sql.SQLException;

@Configuration
public class DataSourceConfig {
    public DataSourceConfig() {
    }

    @Bean
    public ServletRegistrationBean druidStatViewServlet() {
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
        registrationBean.addInitParameter("loginUsername", "admin");
        registrationBean.addInitParameter("loginPassword", "123456");
        registrationBean.addInitParameter("resetEnable", "false");
        return registrationBean;
    }

    @Bean
    public FilterRegistrationBean druidWebStatViewFilter() {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean(new WebStatFilter(), new ServletRegistrationBean[0]);
        registrationBean.addInitParameter("urlPatterns", "/*");
        registrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*");
        return registrationBean;
    }

    @Bean(
            name = {"dataSource"}
    )
    public DataSource druidDataSource(@Value("${datasource.url}") String url, @Value("${datasource.driverClassName}") String driver, @Value("${datasource.username}") String userName, @Value("${datasource.password}") String password, @Value("${datasource.maxActive}") int maxActive, @Value("${datasource.filters}") String filters, @Value("${datasource.initialSize}") int initialSize, @Value("${datasource.minIdle}") int minIdle, @Value("${datasource.maxWait}") int maxWait, @Value("${datasource.timeBetweenEvictionRunsMillis}") int timeBetweenEvictionRunsMillis, @Value("${datasource.minEvictableIdleTimeMillis}") int minEvictableIdleTimeMillis, @Value("${datasource.validationQuery}") String validationQuery, @Value("${datasource.testWhileIdle}") boolean testWhileIdle, @Value("${datasource.testOnBorrow}") boolean testOnBorrow, @Value("${datasource.testOnReturn}") boolean testOnReturn, @Value("${datasource.poolPreparedStatements}") boolean poolPreparedStatements, @Value("${datasource.maxPoolPreparedStatementPerConnectionSize}") int maxPoolPreparedStatementPerConnectionSize, @Value("${datasource.connectionProperties}") String connectionProperties, @Value("${datasource.useGlobalDataSourceStat}") boolean useGlobalDataSourceStat) {
        DruidDataSource dataSource = new DruidDataSource();
        // 数据源主要配置
        dataSource.setUrl(url);
        dataSource.setDriverClassName(driver);
        dataSource.setUsername(userName);
        dataSource.setPassword(password);
        // 数据源补充配置
        dataSource.setMaxActive(maxActive);
        dataSource.setInitialSize(initialSize);
        dataSource.setMinIdle(minIdle);
        dataSource.setMaxWait(maxWait);
        dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
        dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
        dataSource.setValidationQuery(validationQuery);
        dataSource.setTestOnBorrow(testOnBorrow);
        dataSource.setTestOnReturn(testOnReturn);
        dataSource.setTestWhileIdle(testWhileIdle);
        dataSource.setPoolPreparedStatements(poolPreparedStatements);
        dataSource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
        dataSource.setConnectionProperties(connectionProperties);
        dataSource.setUseGlobalDataSourceStat(useGlobalDataSourceStat);
        try {
            dataSource.setFilters(filters);
        } catch (SQLException e) {
            throw new RuntimeException("dataSource of druid init fail!", e);
        }
        return dataSource;
    }

}
